package binarytreelevelordertraversal;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 * https://leetcode.com/problems/binary-tree-level-order-traversal/
 */
class Solution {

    public static void main(String[] args) {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(1);
        DefaultMutableTreeNode c11 = new DefaultMutableTreeNode(2);
        DefaultMutableTreeNode c12 = new DefaultMutableTreeNode(3);
        DefaultMutableTreeNode c112 = new DefaultMutableTreeNode(5);
        root.add(c11);
        root.add(c12);
        c11.add(c112);
        levelOrder(root);
    }

    //bfs
    public static List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new ArrayDeque<TreeNode>();
        List<List<Integer>> resultList = new ArrayList<List<Integer>>();
        queue.add(root);
        while (!queue.isEmpty()){
            int lenth = queue.size();
            List levelList = new ArrayList();
            for(int i=0;i<lenth;i++){
                TreeNode firstNode = queue.poll();
                //业务逻辑
                levelList.add(firstNode);

                Enumeration<TreeNode> clist = firstNode.children();
                while(clist.hasMoreElements()){
                    TreeNode node = clist.nextElement();
                    queue.add(node);
                }
            }
            resultList.add(levelList);
        }
        System.out.println(resultList);
        return resultList;
    }



    //dfs  递归
    public static List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>> resultList = levelOrder(root,0);
        return resultList;
    }

    public static List<List<Integer>> levelOrder(TreeNode root,int level){

        return null;
    }

}